package cn.yunhe.controller;

import cn.yunhe.entity.Role;
import cn.yunhe.validator.RoleValidator;
import org.springframework.stereotype.Controller;
import org.springframework.validation.DataBinder;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

@Controller
public class RoleAction {
    /**
     * 单价数量总价验证   如果总价-(单价*数量)>0  就输出 Field:total,msg:交易金额与购买数量不匹配
     * @param role
     * @param errors
     * @return
     */
    @RequestMapping("/roleValid")
    public ModelAndView roleValid(@Valid Role role, Errors errors) {
        if (errors.hasErrors()) {
            List<FieldError> list = errors.getFieldErrors();
            for (FieldError f : list
            ) {
                System.out.println("Field:" + f.getField() + ",msg:" + f.getDefaultMessage());
            }
        }
        ModelAndView mv = new ModelAndView();
        mv.setViewName("jisuan");
        return mv;
    }

    @InitBinder
    public void initBinder(DataBinder binder) {
        binder.setValidator(new RoleValidator());
    }
}
